Replace generated byte array with go:embed for templates#3597
Replace generated byte array with go:embed for templates#3597cardil wants to merge 8 commits intoknative:mainfrom
Conversation
- generate/templates/main.go: write binary templates.zip instead of Go source - generate/templates/gen.go: extract zip-writing logic (with .gitignore filtering) - generate/templates/check.go: 3-tier verification (exist/stale/hash) - generate/embed.go: //go:embed templates.zip exposes TemplatesZip []byte - delete generate/zz_filesystem_generated.go - .gitignore: add generate/templates.zip - .gitattributes: remove zz_filesystem_generated.go entry - Makefile: update targets/clean to use generate/templates.zip - hack/verify-codegen.sh: use Go checker for templates.zip verification - pkg/functions/templates_embedded.go: run whole package instead of single file - pkg/filesystem/filesystem_test.go: update stale error message Assisted-by: 🤖 Claude Sonnet 4.6
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: cardil The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Would this work even if I imported function as a library (We doing that now in Functions OCP dynamic plugin)? |
|
Sorry, but we tried EDIT: Ah, I see we have a Slack thread about this.
tl;dr, This could perhaps be minimized by splitting the monolithic generated file into a per-language, per-template set of generated files. |
|
I think I have a solution. Will keep you posted... /hold |
- Rename go.mod/go.sum → .embd suffix (go:embed skips dirs with go.mod) - Convert symlinks → .symlink marker files (go:embed drops symlinks) - Add //go:build embd to Go template source files - Add templates/embed.go with //go:embed directive - Add pkg/filesystem/mangling.go: manglingFS transparently reverses mangling at runtime (strips build tags, unmangles names, restores perms) - Add hack/cmd/permissiongen: codegen for templates/.permissions manifest (preserves file permissions lost by embed.FS which returns 0444 for all) - Add hack/cmd/embd: embd/unembd subcommands for Go tooling compatibility (test-go, update-runtime-go targets need real go.mod to work) - Update Makefile: use hack/cmd/embd for test-go and update-runtime-go - Update hack/update-codegen.sh and hack/verify-codegen.sh - Remove generate/ zip infrastructure (embed.go, templates/gen.go etc) - Add .gotagsignore to exclude embd tag from CI build matrix - Export EmbdSuffix, SymlinkSuffix, EmbdBuildConstraint, StripEmbdBuildTag, AddEmbdBuildTag from pkg/filesystem for reuse by hack/cmd/embd - Use go/build/constraint for proper AST-based build tag manipulation Assisted-by: 🤖 Claude Sonnet 4.6
Assisted-by: 🤖 Claude Sonnet 4.6
|
/unhold |
- check-go: use hack/cmd/embd unembd/embd around go vet/lint - Remove make generate/zz_filesystem_generated.go from: update-python-platform.yaml, update-ca-bundle.js, update-quarkus-platform.js, update-springboot-platform.js Assisted-by: 🤖 Claude Opus 4.6
…e-go Scaffolding symlinks (f → ../../http/) cross directory boundaries, so individual subdir unembd leaves targets in mangled state. Unmangle the whole templates/go tree at once instead.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3597 +/- ##
==========================================
+ Coverage 56.30% 56.36% +0.05%
==========================================
Files 180 180
Lines 20549 20836 +287
==========================================
+ Hits 11570 11744 +174
- Misses 7778 7879 +101
- Partials 1201 1213 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Template Go files are now part of the root module (go.mod renamed to go.mod.embd), so go mod tidy resolves their imports.
On Windows, FilesystemFromRepo returns nil for file:// URIs. Guard against nil before passing to NewManglingFilesystem to avoid nil pointer dereference in loadPerms.
|
E2E Runtime failures (go, python, quarkus, rust, typescript) are infra flakes: Docker Hub rate limiting (TOOMANYREQUESTS) and job timeouts. Go E2E TestMatrix_Run and TestMatrix_Deploy all passed — only TestMatrix_Remote was cancelled mid-run. /retest |
|
Thanks for the feedback @matejvasek @lkingland. Your comments were about the earlier version of this PR, which took a different approach. The PR has been fully reworked since then. @matejvasek good point about library import. The current approach uses @lkingland you're right that raw
A The main motivation was merge conflicts. The old The tradeoff is the mangling tooling ( |
Summary
Replaces the codegen-based template embedding with direct
//go:embed templates/, eliminating the 14K-line generated filegenerate/zz_filesystem_generated.goand the codegen tool that produced it.Problem
The old approach used
generate/templates/main.goto walk thetemplates/directory, zip its contents in memory, and write the result as a Go byte array literal (var TemplatesZip) intogenerate/zz_filesystem_generated.go(~14K lines of hex). This required running codegen before any build or test, produced a large committed generated file, and made it harder to inspect embedded template contents or review template diffs.Solution
Use
//go:embeddirectly on thetemplates/directory tree, working around two hardgo:embedlimitations:go.modare silently skipped → renamego.mod/go.sumtogo.mod.embd/go.sum.embd.symlinkplain-text marker files containing the target pathA new
manglingFSwrapper inpkg/filesystemtransparently reverses this mangling at runtime:*.embdfiles are exposed with their real names*.symlinkfiles are presented as symlink directory entries//go:build embdconstraints are stripped from Go source file contentembed.FSwhich returns 0444 for everything) are restored from a committedtemplates/.permissionsmanifestNew Tools
hack/cmd/embd—embd/unembdsubcommands to temporarily unmangle a template directory so normal Go tooling (go mod tidy,go get,go test) can operate on it, then re-mangle it. Uses the same constants/logic asmanglingFSto keep mangling rules in sync.hack/cmd/permissiongen— codegen tool that scanstemplates/and generatestemplates/.permissionslisting non-standard file permissions (only executable files differ from the default 0644). Run viago run ./hack/cmd/permissiongen.Build tag approach
Go template source files are tagged with
//go:build embdso the Go toolchain does not attempt to compile them as part of the main module. Thego/build/constraintpackage is used for correct AST-based manipulation of build tags (preserving any existing tags on round-trips throughembd/unembd).Changes
templates/embed.go—//go:embeddirectivepkg/filesystem/mangling.go—manglingFSruntime FS wrapperpkg/functions/templates_embedded.go— wires upmanglingFShack/cmd/embd/main.go— embd/unembd CLI toolhack/cmd/permissiongen/main.go— permissions manifest codegentemplates/.permissions— committed generated manifestMakefile— updatedcheck-go,test-goandupdate-runtime-gotargetshack/update-codegen.sh/hack/verify-codegen.sh— updated.gotagsignore— excludesembdfrom CI build tag matrixgenerate/zz_filesystem_generated.go,generate/templates/main.goAssisted-by: 🤖 Claude Opus/Sonnet 4.6